package com.taobao.ic.ticket.test;

import java.util.Date;

import junit.framework.Assert;

import org.junit.Test;

import com.taobao.ic.ticket.constants.TicketConstants;
import com.taobao.ic.ticket.domain.PagerDO;
import com.taobao.ic.ticket.domain.SearchTicketConditionDO;
import com.taobao.ic.ticket.domain.TicketDO;
import com.taobao.ic.ticket.domain.TicketQueryOptionsDO;
import com.taobao.ic.ticket.domain.TicketSkuDO;
import com.taobao.ic.ticket.result.ResultDO;
import com.taobao.ic.ticket.result.SearchResultDO;
import com.taobao.ic.ticket.utils.CheckUtils;

public class TicketTestCase extends SpringTestCase {

	@Test
	public void testTicketPublish() {
		Assert.assertNotNull(ticketService);

		TicketDO ticket = new TicketDO();
		ticket.setArriveStationId(1000);
		ticket.setArriveTime(new Date());
		ticket.setBeginStationId(1001);
		ticket.setBeginTime(new Date());
		ticket.setSellerId(1001);
		ticket.setTicketStatus(TicketConstants.TICKET_STATUS_NORMAL);
		ticket.setTrainNum("G28");
		ticket.setTrainType(TicketConstants.TRAIN_TYPE_G);

		TicketSkuDO ticketSkuA = new TicketSkuDO();
		ticketSkuA.setPrice(1000);
		ticketSkuA.setQuantity(500);
		ticketSkuA.setTicketType(TicketConstants.TICKET_TYPE_SOFTBED);
		ticket.getTicketSkuList().add(ticketSkuA);

		TicketSkuDO ticketSkuB = new TicketSkuDO();
		ticketSkuB.setPrice(2000);
		ticketSkuB.setQuantity(100);
		ticketSkuB.setTicketType(TicketConstants.TICKET_TYPE_HARDBED);
		ticket.getTicketSkuList().add(ticketSkuB);

		ResultDO<TicketDO> result = ticketService.publishTicket(ticket);
		Assert.assertTrue(result.isSuccess());

		for (TicketSkuDO sku : result.getModel().getTicketSkuList()) {
			Assert.assertTrue(sku.getSkuId() > 0);
		}

	}

	@Test
	public void testForSearch() {
		SearchTicketConditionDO condition = new SearchTicketConditionDO();
		PagerDO pager = new PagerDO();
		pager.setPageSize(3);
		pager.setCurrentPage(1);
		condition.setBeginStationId(1016);
		condition.setArriveStationId(1032);
		condition.setBeginDateFrom(new Date(0));
		condition.setBeginDateTo(new Date());
		// condition.addTicketType(TicketConstants.TICKET_TYPE_HARDBED);
		 condition.addTrainType(TicketConstants.TRAIN_TYPE_D);
		 condition.addTrainType(TicketConstants.TRAIN_TYPE_G);

		SearchResultDO<TicketDO> result = ticketService.searchTicket(condition,
				pager);
		Assert.assertTrue(result.isSuccess());
		Assert.assertTrue((result.getTotal() > 0));
		Assert.assertFalse(result.getModel().isEmpty());
		for (TicketDO ticket : result.getModel()) {
			Assert.assertTrue(CheckUtils.isIn(ticket.getTrainType(),
					TicketConstants.TRAIN_TYPE_D, TicketConstants.TRAIN_TYPE_G));
			Assert.assertFalse(CheckUtils.isIn(ticket.getTrainType(),
					TicketConstants.TRAIN_TYPE_K, TicketConstants.TRAIN_TYPE_T));
		}
	}
	
	@Test
	public void testForGetTicketById() {
		ResultDO<TicketDO> result = ticketService.queryTicketById(100104l, TicketQueryOptionsDO.QUERY_FOR_DETAIL);
		Assert.assertTrue(result.isSuccess());
		for (TicketSkuDO sku : result.getModel().getTicketSkuList()) {
			Assert.assertTrue(sku.getQuantity() > 0);
		}
	}
	

}
